<?php

namespace App\Service\Activity;

use App\Helpers\AliyunUtil;
use App\Model\Act\WishTree;
use App\Model\UserProfile;
use App\Service\BaseService;

/**
 * 许愿树排行
 */
class WishTreeRankService extends BaseService
{
    public function handle()
    {
        $ret = $this->getRankInfo();
        return $ret;
    }

    function getRankInfo()
    {
        $rankList = WishTree::getRankList();
        $userIds = array_keys($rankList);
        // $userIds[] = $this->userId;
        $profiles = UserProfile::getUsersProfile($userIds, true);
        $data['list'] = [];
        $rank = 1;
        // $selfAvatar = UserProfile::buildAvatar($profiles[$this->userId]['avatar'], 200);
        foreach ($rankList as $userId => $num) {
            if ($userId == $this->userId) $data['my_info'] = [
                'rank' => $rank,
                'wish_num' => $num,
                // 'avatar' => UserProfile::buildAvatar($profiles[$userId]['avatar'], 200)
            ];
            $data['list'][] = [
                'user_id' => $userId,
                'nick_name' => $profiles[$userId]['nick_name'],
                'avatar' => UserProfile::buildAvatar($profiles[$userId]['avatar'], 200),
                'wish_num' => $num,
                'rank' => $rank++,
            ];
        }
        if (!isset($data['my_info'])) $data['my_info'] = [
            'rank' => 0,
            'wish_num' => 0,
            // 'avatar' => $selfAvatar,
        ];
        return $data;
    }
}
